<template>
  <div class="life-board">
    <div class="life-header">
      <h1>生活服务</h1>
    </div>

    <div class="life-content">
      <div class="life-grid">
        <div v-for="life in lifeList" :key="life.id" class="life-card">
          <div class="life-image">
            <img :src="life.img" :alt="life.title">
          </div>
          <div class="life-details">
            <h3>{{ life.title }}</h3>
            <p class="life-excerpt">{{ life.content.substring(0, 100) }}...</p>
            <div class="life-meta">
              <span class="life-date">{{ formatDate(life.updatedAt) }}</span>
            </div>
            <button class="read-more" @click="showLifeDetail(life)">阅读全文</button>
            <button class="read-more" v-show="!life.isReservation" @click="reservation(life)">预约</button>
            <button class="read-more" v-show="life.isReservation" @click="reservation(life)">取消预约</button>
          </div>
        </div>
      </div>
    </div>

    <!-- 生活服务详情弹窗 -->
    <div v-if="selectedLife" class="life-modal" @click.self="selectedLife = null">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h2>{{ selectedLife.title }}</h2>
          <button class="close-btn" @click="selectedLife = null">×</button>
        </div>
        <div class="modal-body">
          <img :src="selectedLife.img" :alt="selectedLife.title" class="life-detail-image">
          <div class="life-meta">
            <span class="life-date">{{ formatDate(selectedLife.updatedAt) }}</span>
          </div>
          <div class="life-content-full">
            {{ selectedLife.content }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LifeBoard',
  data() {
    return {
      error: null,
      lifeList: [],
      selectedLife: null,
      lifePreferences: ''
    }
  },

  methods: {
    async fetchLife() {
      const user = JSON.parse(localStorage.getItem('user'));
      const ownerId = user ? user.ownerId : null;
      try {
        const ownerResponse = await fetch(`http://localhost:8080/api/owners/${ownerId}`);
        if (!ownerResponse.ok) throw new Error('获取业主信息失败');
        const userInfo = await ownerResponse.json();
        this.lifePreferences = userInfo.lifePreferences;
        const lifeResponse = await fetch(`http://localhost:8080/api/life/tag/${userInfo.lifePreferences}`);
        if (!lifeResponse.ok) throw new Error('获取生活服务信息失败');
        this.lifeList = await lifeResponse.json();
      } catch (err) {
        this.error = err.message;
      }
    },

    formatDate(dateString) {
      if (!dateString) return '待定'
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
      })
    },

    showLifeDetail(life) {
      this.selectedLife = life;
    },

    /*getLifeImage(lifePreferences) {
      const images = {
        politics: 'https://img.freepik.com/free-photo/group-people-working-together-office_23-2149800867.jpg',
        sports: 'https://img.freepik.com/free-photo/sports-tools_53876-138077.jpg',
        technology: 'https://img.freepik.com/free-photo/medium-shot-man-wearing-vr-glasses_23-2149126949.jpg',
        entertainment: 'https://img.freepik.com/free-photo/excited-audience-watching-confetti-fireworks-having-fun-music-festival-night-copy-space_637285-559.jpg',
        business: 'https://img.freepik.com/free-photo/business-people-shaking-hands-together_53876-30568.jpg',
        health: 'https://img.freepik.com/free-photo/doctor-with-stethoscope-hands-hospital-background_1423-1.jpg',
        science: 'https://img.freepik.com/free-photo/scientist-working-laboratory_23-2148880401.jpg',
        education: 'https://img.freepik.com/free-photo/medium-shot-woman-teaching-class_23-2149038403.jpg'
      };

      return images[lifePreferences] || 'https://img.freepik.com/free-photo/aerial-view-residence-building_23-2149360330.jpg';
    },*/

    async reservation(life) {
      try {
        life.isReservation = !life.isReservation
        const response = await fetch(`http://localhost:8080/api/life/${life.id}`, {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(life)
        });
        if (!response.ok) {
          const errorText = await response.text();
          throw new Error(errorText || '更新生活服务信息失败');
        }
        const updatedLife = await response.json();
        this.success = '生活服务信息更新成功！';
        localStorage.setItem('life', JSON.stringify({
          isReservation: updatedLife.isReservation,
        }));
      } catch (err) {
        this.error = err.message;
      }
    },
  },

  created() {
    this.fetchLife()
  }
}
</script>

<style scoped>
.life-board {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.life-header {
  margin-bottom: 30px;
}

.life-header h1 {
  font-size: 2rem;
  color: var(--text-color);
  margin-bottom: 20px;
}

.life-filter {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

.life-filter span {
  color: var(--gray);
  font-weight: 500;
}

.filter-btn {
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.filter-btn.active {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.filter-btn:hover:not(.active) {
  background: #f8f9fa;
}

.life-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 25px;
}

.life-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease;
}

.life-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.life-image {
  position: relative;
  height: 200px;
}

.life-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.life-category {
  position: absolute;
  bottom: 10px;
  left: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 0.8rem;
}

.life-details {
  padding: 20px;
}

.life-details h3 {
  margin: 0 0 15px 0;
  font-size: 1.2rem;
  color: var(--text-color);
}

.life-excerpt {
  color: var(--gray);
  margin-bottom: 15px;
  line-height: 1.5;
}

.life-meta {
  display: flex;
  justify-content: space-between;
  color: var(--gray);
  font-size: 0.85rem;
  margin-bottom: 15px;
}

.read-more {
  background: var(--primary-color);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s ease;
  margin-right: 15px;
}

.read-more:hover {
  background: var(--secondary-color);
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px 0;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-top-color: var(--primary-color);
  animation: spin 1s ease-in-out infinite;
  margin-bottom: 20px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.error-message, .empty-state {
  text-align: center;
  padding: 50px 0;
  color: var(--gray);
}

/* 生活服务详情弹窗 */
.life-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 800px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e2e8f0;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--gray);
}

.modal-body {
  padding: 20px;
}

.life-detail-image {
  width: 100%;
  max-height: 400px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 20px;
}

.life-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 20px;
}

.life-category-badge {
  background: var(--primary-color);
  color: white;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 0.8rem;
}

.life-content-full {
  line-height: 1.8;
  color: var(--text-color);
  white-space: pre-line;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .life-grid {
    grid-template-columns: 1fr;
  }

  .life-filter {
    justify-content: center;
  }

  .modal-content {
    width: 95%;
  }
}

.recommend-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  background: #ff4757;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: bold;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>